package com.yunhe.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhe.common.QueryPageParam;
import com.yunhe.common.Result;
import com.yunhe.entity.Storage;
import com.yunhe.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
/**
 * 控制层代码
 * 调用：业务层，业务层调用dao层，被ServiceImpl类封装了。
 */
@RestController
@RequestMapping("/storage")
@Api(value = "/storage",description = "仓库信息管理")
public class StorageController {
    @Autowired
    StorageService storageService;

    /**
     * 针对仓库的增加业务
     * @param storage
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("仓库信息保存")
    public Result save(@RequestBody Storage storage){
        return storageService.save(storage)?Result.success():Result.fail();
    }

    @GetMapping("/list")
    @ApiOperation("获取所有仓库列表")
    public Result list(){
        return Result.success(storageService.list());
    }
    /**
     * 针对仓库的编辑业务
     * @param storage
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("仓库信息编辑")
    public Result update(@RequestBody Storage storage){
        return storageService.updateById(storage)?Result.success():Result.fail();
    }

    @GetMapping("/del")
    @ApiImplicitParam(dataType = "Long",paramType = "query",name = "id",value = "仓库编号",required = true)
    public Result del(String id){
        return storageService.removeById(id)?Result.success():Result.fail();
    }

    /**
     * 请求体 请求类型 请求类型参数名
     * @param query
     * @return
     */
    @PostMapping("/listPage")
    @ApiOperation("分页查询和模糊查询")
    public Result listPage(@RequestBody QueryPageParam query){
        //获取响应的参数：
        HashMap param=query.getParam("param");
        String name= (String) param.get("name");

        //分页对象;设置额前端页面传递过来的页码和每页显示的条数；
        Page<Storage>page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        //包装器查询
        LambdaQueryWrapper<Storage>queryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)&&!"null".equals(name))
            queryWrapper.like(Storage::getName, name);
        //查询：利用业务层接口对象查询;
        Page<Storage> result = storageService.page(page, queryWrapper);

        return Result.success(result.getRecords(), result.getTotal());
    }
}